Ext.define('Admin.view.authentication.AuthenticationController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.authentication',

    onLoginButton: function (btn) {
        var me = this;

        Ext.Ajax.request({
            url: 'system/login',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                username: btn.up("form").getForm().findField("username").getValue(),
                password: btn.up("form").getForm().findField("password").getValue(),
                rememberMe: btn.up("form").getForm().findField("rememberMe").getValue()
            },
            success: function (response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code == 200) {
                    window.location.reload();
                    me.redirectTo('dashboard', true);
                } else {
                    Ext.Msg.alert('警告', '帐号或密码错误，请重新输入！');
                }
            }
        });
    },

    onSignupClick: function (btn) {
        var me = this;

        Ext.Ajax.request({
            url: 'system/register',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                username: btn.up("form").getForm().findField("username").getValue(),
                password: btn.up("form").getForm().findField("password").getValue(),
                fullname: btn.up("form").getForm().findField("fullName").getValue(),
                email: btn.up("form").getForm().findField("email").getValue()
            },
            success: function (response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code == 200) {
                    me.redirectTo('login', true);
                } else {
                    Ext.Msg.alert('警告', json.message);
                }
            }
        });
    },

    onNewAccount: function () {
        this.redirectTo('register', true);
    },

    onResetClick: function (btn) {

    }
});